package org.jxf.zhaohuo.utils;

import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.util.Base64;
import android.util.Log;

public class HttpUtils {

	public static String getHtml4Url(URI uri) {
		HttpClient http = new DefaultHttpClient();

		try {

			HttpGet httpget = new HttpGet(uri);

			HttpEntity entity = null;

			HttpResponse response = http.execute(httpget);
			entity = response.getEntity();
			return EntityUtils.toString(entity);

		} catch (Exception e) {
			Log.i("hello", e.getMessage(), e);
			return null;
		}

	}

	public static String getHtml4Url(String urls) throws Exception {
		URL url;

		try {
			HttpClient http = new DefaultHttpClient();

			//Log.i("hello", urls);
			url = new URL(urls);
			URI uri = new URI(url.getProtocol(), url.getUserInfo(),
					url.getHost(), url.getPort(), url.getPath(),
					url.getQuery(), null);

			HttpGet httpget = new HttpGet(uri);
			
	
			httpget.addHeader("charset", "GB2312");
			HttpEntity entity = null;

			HttpResponse response = http.execute(httpget);
			entity = response.getEntity();
			return EntityUtils.toString(entity);

		} catch (Exception e) {
			Log.i("hello", e.getMessage(), e);
			throw (e);
		}

	}

	public static Map gps2baidu(String lng, String lat) {
		Stopwatch watch = new Stopwatch("gps2baidu");
		watch.start();
		Map m = new HashMap();
		try {
			String url = 
					"http://api.map.baidu.com/ag/coord/convert?from=0&to=4&mode=1&x="
							+ lng + "&y=" + lat;
			
			String result = getHtml4Url(url);
			String s = result;
			s = s.substring(1, s.length() - 1);

			JSONObject jsonObject = new JSONObject(s);
			
			String errorCode = jsonObject.get("error").toString();
			
			if ("0".equals(errorCode)){
				String x1 = jsonObject.get("x").toString();
				String y1 = jsonObject.get("y").toString();

				x1 = new String(Base64.decode(x1, Base64.DEFAULT));
				y1 = new String(Base64.decode(y1, Base64.DEFAULT));

				m.put("baidu_lat", y1);
				m.put("baidu_lon", x1);
				
			}else{
				m.put("baidu_lat", "0");
				m.put("baidu_lon", "0");
			}
			
		} catch (Exception ex) {
			Log.i(HttpUtils.class.getName(),ex.getMessage(),ex);
			m.put("baidu_lat", "0");
			m.put("baidu_lon", "0");

		}
		watch.stop();
		return m;

	}

	public static InputStream getInputStream4Url(String urls){
		URL url;

		try {
			HttpClient http = new DefaultHttpClient();

			//Log.i("hello", urls);
			url = new URL(urls);
			URI uri = new URI(url.getProtocol(), url.getUserInfo(),
					url.getHost(), url.getPort(), url.getPath(),
					url.getQuery(), null);

			HttpGet httpget = new HttpGet(uri);

			HttpEntity entity = null;

			HttpResponse response = http.execute(httpget);
			entity = response.getEntity();
			return entity.getContent();

		} catch (Exception e) {
			Log.i("hello", e.getMessage(), e);
			return null;
		}
		
	}
}
